Completed
Push — master ( 66108c...e529c4 )
by Andres
33s
created

angular.service(ꞌstateꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 2
1
/**
2
 state
3
 This service handles all the mutable data of the game, most importantly the player data.
4
 It also binds together the main-loop with each component by keeping a list of update
5
 functions where components can register themselves.
6
7
 @namespace Services
8
 */
9
'use strict';
10
11
angular
12
  .module('game')
13
  .service('state', ['data',
14
  function(data) {
15
    let sv = this;
16
    sv.hoverElement = '';
17
    sv.export = '';
18
    sv.player = {};
19
    sv.loading = true;
20
    sv.processingOffline = false;
21
    sv.offlineCyclesTotal = 0;
22
    sv.offlineCyclesCurrent = 0;
23
    sv.cancelOffline = false;
24
    sv.toast = [];
25
    sv.isToastVisible = false;
26
    let newElements = [];
27
    let updateFunctions = {};
28
    sv.reactionsCache = {};
29
    sv.redoxesCache = {};
30
31
    sv.deleteToast = function(currentTs, eventTs) {
32
      if(currentTs-eventTs >= 350){
33
        sv.toast.shift();
34
        if (sv.toast.length > 0) {
35
          sv.isToastVisible = true;
36
          window.requestAnimationFrame((ts) => sv.removeToast(ts, performance.now()));
0 ignored issues
show
Bug introduced by
The variable performance seems to be never declared. If this is a global, consider adding a /** global: performance */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
37
        }
38
      }else{
39
        window.requestAnimationFrame((ts) => sv.deleteToast(ts, eventTs));
40
      }
41
    };
42
43
    sv.removeToast = function(currentTs, eventTs) {
44
      if(currentTs-eventTs >= 2500){
45
        sv.isToastVisible = false;
46
        window.requestAnimationFrame((ts) => sv.deleteToast(ts, performance.now()));
0 ignored issues
show
Bug introduced by
The variable performance seems to be never declared. If this is a global, consider adding a /** global: performance */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
47
      }else{
48
        window.requestAnimationFrame((ts) => sv.removeToast(ts, eventTs));
49
      }
50
    };
51
52
    sv.addToast = function (t) {
53
      sv.toast.push(t);
54
      if (sv.toast.length === 1) {
55
        sv.isToastVisible = true;
56
        window.requestAnimationFrame((ts) => sv.removeToast(ts, performance.now()));
0 ignored issues
show
Bug introduced by
The variable performance seems to be never declared. If this is a global, consider adding a /** global: performance */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
57
      }
58
    };
59
60
    sv.init = function() {
61
      sv.hoverElement = '';
62
      sv.export = '';
63
      sv.toast = [];
64
      sv.isToastVisible = false;
65
      newElements = [];
66
    };
67
68
    sv.hasNew = function(entry) {
69
      return newElements.indexOf(entry) !== -1;
70
    };
71
72
    sv.addNew = function(entry) {
73
      newElements.push(entry);
74
    };
75
76
    sv.removeNew = function(entry) {
77
      if (newElements.indexOf(entry) !== -1) {
78
        newElements.splice(newElements.indexOf(entry), 1);
79
      }
80
    };
81
82
    sv.elementHasNew = function(element) {
83
      let includes = data.elements[element].includes;
84
      for (let key in includes) {
85
        if (sv.hasNew(includes[key])) {
86
          return true;
87
        }
88
      }
89
      return false;
90
    };
91
92
    sv.registerUpdate = function(name, func){
93
      updateFunctions[name] = func;
94
    };
95
96
    sv.update = function(player){
97
      for(let func in updateFunctions){
98
        updateFunctions[func](player);
99
      }
100
    };
101
  }]);
102